home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvtools / examples / programs / text_editor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-08  |  13.4 KB  |  433 lines

  1. #ifndef lint
  2. static char SccsId[]= "@(#)text_editor.c    V1.26    3/13/95";
  3. #endif
  4. /*
  5. |    file name - text_editor.c
  6. |===================================================================
  7. |
  8. |       This is an example program that demonstrates how to use a
  9. |    textedit input object. It shows how to load a file into the
  10. |    the editor and save the edited text back into a file.
  11. |       You can type text and save it into a file, or you can load
  12. |    text from a file, edit it, and save it back into the same file
  13. |    or into a different one.
  14. |       To save the text editor's current content into a file, type
  15. |    the filename into the single line text entry field and
  16. |    select the "Save" button.
  17. |       To load a file into the editor, type the filename of the file
  18. |    to load and select the "Load" button.
  19. |       After you save or load a file, a "Saved," "Loaded," or
  20. |    "Error" message appears.
  21. |       To exit from the program, select the "Quit" button.
  22. |       To display help information about the editor commands, select
  23. |    the "Help" button.
  24. |       This example can be modified to suit your needs.
  25. |
  26.  |===================================================================
  27. */
  28. #include <windows.h>
  29. /*
  30.  *  DV-Tools header files
  31.  */
  32. #include "std.h"                /* <stdio.h> etc., scalar & macro definitions */
  33. #include "dvstd.h"              /* public types & constants */
  34. #include "dvtools.h"            /* constants used by T routines */
  35. #include "dvGR.h"               /* constants used by window mgt & GR routines */
  36. #include "VOstd.h"              /* constants used by VO & VOob routines */
  37. #include "Tfundecl.h"           /* T routines (screens, drawports & views) */
  38. #include "VOfundecl.h"          /* VO routines (objects) */
  39. #include "VUerfundecl.h"        /* VUer routines (event handling routines) */
  40. #include "VGfundecl.h"          /* VG routines (get info from dgp & vdp) */
  41. #ifdef VMS
  42. #include <file.h>
  43. #else
  44. #include <fcntl.h>
  45. #endif
  46.  
  47. /* Constants */
  48. #define  DVPATH            (char *)NULL
  49. #define  DISPFORMS_STB     (char *)NULL
  50. #define  DVDEVICE          (char *)NULL
  51. #define  DVCOLORTABLE      (char *)NULL
  52. #define  VIEW_NAME         "editor.v"
  53. #define  SCREEN_VIEWPORT   (RECTANGLE *)NULL
  54. #define  DRAWING_VIEWPORT  (RECTANGLE *)NULL
  55.  
  56. /* Define menu constants */
  57. #define SAVE 1
  58. #define LOAD 2
  59. #define QUIT 3
  60.  
  61. #define TOO_LONG 2
  62.  
  63. /* Define global variables */
  64. float menu_value;               /* buffer for menu input object */
  65. int max_length;                 /* max. length of string */
  66. char filename[101] =
  67. {0};                            /* filename to load/save */
  68. char text_var[1001] =
  69. {0};                            /* buffer for text editor input object */
  70. int Quit = NO;                  /* flag to quit the program */
  71.  
  72. /*
  73.  *  Information passed to service result routine
  74.  */
  75. struct Set
  76. {
  77.   DRAWPORT drawport;
  78.   OBJECT editor_obj, menu_obj, message_obj, filename_obj;
  79. };
  80.  
  81. /* Functions defined in text_editor.c */
  82. LOCAL  int HandleSaveLoad V_P_((OBJECT client, EVENT_REQUEST request, 
  83.   int label, OBJECT location, ADDRESS argblock));
  84. LOCAL  int SaveFilename V_P_((void));
  85. LOCAL  int LoadFilename V_P_((void));
  86. /***************** End Function Declarations *************/
  87.  
  88. /*
  89.  *   MAIN PROGRAM
  90.  */
  91. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  92.                      LPSTR lpCmdLine,  int nCmdShow  )
  93. {
  94.   INT argc = 0;
  95.   CHAR **argv;
  96.   /*
  97.    *  program arguments
  98.    *    argv[1] - display device name (default is to use DVDEVICE)
  99.    */
  100.  
  101.   /* Define & initialize device name and view filename */
  102.   char *device_name = DVDEVICE; /* default device name */
  103.   char *view_name = VIEW_NAME;  /* default view name */
  104.  
  105.   /* Define display variables */
  106.   OBJECT screen;                /* display device, the window */
  107.   VIEW view;                    /* picture representation of the view file */
  108.  
  109.   /* Control loop variables */
  110.   OBJECT location;              /* the event representation */
  111.  
  112.   /* Input object related variables */
  113.   OBJECT drawing;               /* graphical representation of screen */
  114.   ADDRESS *vdplist;             /* variable descriptor list for menu */
  115.   int numvars,                  /* number of vdps in list */
  116.     dim;                        /* dimension of vdp assigned to text editor */
  117.   struct Set set;
  118.  
  119.   /*--------------------
  120.    *   Initialization
  121.    *
  122.    *   TInit:  perform the initialization of DV-Tools
  123.    *           TInit reads your configuration file and any
  124.    *           environment variables or logical names set.
  125.    */
  126.   make_argv(&argc,&argv,GetCommandLine());
  127.   TInit( DVPATH, DISPFORMS_STB );
  128.  
  129.   /*
  130.    *   TscOpenSet: open a device as a screen object using
  131.    *               specified attributes
  132.    *
  133.    *   Set exposure block to YES to insure the window
  134.    *   is ready for drawing when TdpDraw is called.
  135.    */
  136.   if (argc > 1)
  137.     device_name = argv[1];
  138.   screen = TscOpenSet (device_name, DVCOLORTABLE,
  139.                        V_WINDOW_NAME, "text_editor",
  140.                        V_X_EXPOSURE_BLOCK, YES,
  141.                        V_ACTIVE_CURSOR, V_END_OF_LIST);
  142.   if (!screen)
  143.     {
  144.       printf ("Must specify device on command line or");
  145.       printf (" in DataViews configuration file.\n");
  146.       S_EXIT (EXIT_ERR);
  147.     }
  148.  
  149.   /*
  150.    *   VOscWinEventMask:  sets the screen's window event mask
  151.    */
  152.   VOscWinEventMask ((ULONG) V_KEYPRESS | V_BUTTONPRESS |
  153.                             V_MOTIONNOTIFY | V_EXPOSE | V_RESIZE,
  154.             (ULONG) 0);
  155.  
  156.   /*
  157.    *   TviLoad:   Load a view in from a file
  158.    *   TdpCreate: Create a DV-Tools window, a drawport.
  159.    *              The drawport is attached to the screen object
  160.    *              specified while view specifies the view to be
  161.    *              displayed on the screen.
  162.    */
  163.   view = TviLoad (view_name);
  164.   if (!view)
  165.     {
  166.       printf ("Could not load view from file ");
  167.       printf ("%s.\n", view_name);
  168.       S_EXIT (EXIT_ERR);
  169.     }
  170.   set.drawport = TdpCreate (screen, view, SCREEN_VIEWPORT, DRAWING_VIEWPORT);
  171.  
  172.   /*
  173.    *  TviGetDrawing:     Gets a view's drawing object
  174.    *  TdrGetNamedObject: Gets a named object from a
  175.    *                      drawing.
  176.    *
  177.    *  Get objects from the view.
  178.    */
  179.   drawing = TviGetDrawing (view);
  180.   set.editor_obj = TdrGetNamedObject (drawing, "editor");
  181.   set.menu_obj = TdrGetNamedObject (drawing, "menu");
  182.   set.filename_obj = TdrGetNamedObject (drawing, "filename");
  183.   set.message_obj = TdrGetNamedObject (drawing, "message");
  184.  
  185.   /*
  186.    *   VOinGetVarList:  Gets a variable descriptor list of
  187.    *                    the input object.
  188.    *   TvdPutBuffer:    Sets a new variable descriptor buffer
  189.    *
  190.    *  Get the variables associated with the menu input object
  191.    *  used for saving, loading and quiting as well as the variable
  192.    *  associated with the text entry input object used to
  193.    *  enter the filename for loading and saving.
  194.    */
  195.   VOinGetVarList (set.menu_obj, &vdplist, &numvars);
  196.   TvdPutBuffer (vdplist[0], (ADDRESS) & menu_value);
  197.   VOinGetVarList (set.filename_obj, &vdplist, &numvars);
  198.   TvdPutBuffer (vdplist[0], filename);
  199.  
  200.   /*
  201.    *  VGvddim:  Gets the dimensions of a variable descriptor
  202.    *
  203.    *  Get the vdp from the text editor and set a new buffer for
  204.    *  the vdp. Determine the maximum lenght of the character
  205.    *  string allowed in the multi line text editor buffer.
  206.    */
  207.   VOinGetVarList (set.editor_obj, &vdplist, &numvars);
  208.   VGvddim (vdplist[0], &dim, &dim, &max_length);
  209.   TvdPutBuffer (vdplist[0], text_var);
  210.  
  211.   /*
  212.    *   VUerServiceResultPost:  Post a service result request
  213.    *                           with the event handler.
  214.    *
  215.    *   Post a service result request which monitors the
  216.    *   text menu input object. The request specifies the
  217.    *   type of the service result flag to be generated as
  218.    *   an INPUT_DONE.  INPUT_DONE indicates an input sequence
  219.    *   has been completed (a menu selection was made).
  220.    */
  221.   VUerServiceResultPost ((OBJECT)1, (VUERFCNFUNPTR)HandleSaveLoad, (ADDRESS) & set, 
  222.                          (int)sizeof (set), set.menu_obj, (int)INPUT_DONE, (int)0);
  223.  
  224.   /*
  225.    *   TdpDraw:  Draw the contents of the drawport
  226.    */
  227.   TdpDraw (set.drawport);
  228.  
  229.   /*--------------------
  230.    *   Control loop
  231.    *
  232.    *   Poll the event queue for locator events. If the key
  233.    *   represents the character 'q' or 'Q' or the right
  234.    *   mouse button then quit the program. Meanwhile,
  235.    *   continue to update the data buffers and draw the
  236.    *   next iteration of the drawport.
  237.    */
  238.   FOREVER
  239.   {
  240.     /*
  241.      *  VOscWinEventPoll:  Poll for the next window event.
  242.      *                     The polling mode used is V_WAIT.
  243.      *                     Therefore, VOscWinEventPoll does not
  244.      *                     return until a masked event is
  245.      *                     generated.  V_WAIT always produces
  246.      *                     a valid location object.
  247.      */
  248.     location = VOscWinEventPoll (V_WAIT);
  249.  
  250.     /*
  251.      *  VOloType:  returns the type of event.  These types
  252.      *             match event types specified in VOscWinEventMask.
  253.      */
  254.     switch (VOloType (location))
  255.       {
  256.  
  257.       case V_RESIZE:
  258.         /*
  259.          *  The window size has been changed.
  260.          *  TscReset:  Resets all screen drawports after
  261.          *             window resizing
  262.          */
  263.         TscReset (screen);
  264.         break;
  265.  
  266.       case V_EXPOSE:
  267.         /*
  268.          *  VOloRegion:  Returns a rectangle representing the
  269.          *               exposed region on the screen.
  270.          *  TscRedraw:   After erasing, redraws all the drawports
  271.          *               in the screen.
  272.          *  A portion of the window has been exposed and needs
  273.          *  to be redrawn.
  274.          */
  275.         TscRedraw (screen, VOloRegion (location));
  276.         break;
  277.  
  278.       case V_KEYPRESS:
  279.       case V_BUTTONPRESS:
  280.       case V_MOTIONNOTIFY:
  281.         VUerHandleLocEvent (location);
  282.         /* Updates editor object only if it was changed after loading
  283.            the new text. */
  284.         TdpDrawNext (set.drawport);
  285.         break;
  286.  
  287.       default:
  288.         break;
  289.       }
  290.     if (Quit == YES)
  291.       break;
  292.   }
  293.  
  294.   /*--------------------
  295.     *   Termination
  296.     *
  297.     *   TscErase:     Erase the entire screen in the default
  298.     *                 background color
  299.     *   TdpDestroy:   Destroy the drawport,
  300.     *   TviDestroy:   Destroy the view, freeing the allocated memory
  301.     *   TscCloseCurrentScreen:  Close the current display screen
  302.     *   TTerminate:   Perform the clean-up for DV-Tools
  303.     */
  304.   TscErase (screen);
  305.   TdpDestroy (set.drawport);
  306.   TviDestroy (view);
  307.   TscCloseCurrentScreen ();
  308.   TTerminate ();
  309.   return EXIT_OK;
  310. }
  311.  
  312. /*-----------------
  313.  *   HandleSaveLoad  --  Check users selection for loading or saving
  314.  *   multiple lines of text entered by the user.
  315.  */
  316. /*ARGSUSED */
  317. LOCAL int 
  318. HandleSaveLoad (client, request, label, location, argblock)
  319.      OBJECT client;
  320.      EVENT_REQUEST request;
  321.      int label;
  322.      OBJECT location;
  323.  
  324.      ADDRESS argblock;
  325. {
  326.   int saveload;
  327.   char *message = NULL;
  328.   struct Set *set = (struct Set *)argblock;;  
  329.  
  330.   /*
  331.    * Check selection made by user to either save or load.
  332.    */
  333.   switch ((int) menu_value)
  334.     {
  335.     case SAVE:
  336.       saveload = SaveFilename ();
  337.       if (saveload)
  338.         message = "File was saved successfully";
  339.       else
  340.         message = "Warning: error occurred when saving file";
  341.       break;
  342.  
  343.  
  344.     case LOAD:
  345.       saveload = LoadFilename ();
  346.       if (saveload == DV_SUCCESS)
  347.         message = "File was loaded successfully";
  348.       else if (saveload == TOO_LONG)
  349.         message = "File is too long, only part was loaded. Don't save it!";
  350.       else
  351.         message = "Warning: error occurred when loading file";
  352.       break;
  353.  
  354.     case QUIT:
  355.       Quit = YES;
  356.       return INPUT_DONE;
  357.     }
  358.  
  359.   if (saveload != DV_FAILURE)      /* To set an initial content to restore to */
  360.     TdpDrawObject (set->drawport, set->editor_obj);
  361.  
  362.   /* Display message string to user */
  363.   TdpEraseObject (set->drawport, set->message_obj);
  364.   VOvtSetString (set->message_obj, message);
  365.   TdpDrawObject (set->drawport, set->message_obj);
  366.  
  367.   return INPUT_DONE;
  368. }
  369.  
  370.  
  371. /*---------------
  372.  *   SaveFilename  --  Save text entered by user in text edit
  373.  *   input object in specified file.
  374.  */
  375. LOCAL int
  376. SaveFilename ()
  377. {
  378.   FILE *fp;
  379.  
  380.   if (strcmp (filename, "") == 0)
  381.     return DV_FAILURE;
  382.   fp = fopen (filename, "w");
  383.   if (fp == (FILE *) 0)
  384.     return DV_FAILURE;
  385.  
  386.   fwrite (text_var, strlen (text_var), 1, fp);
  387.   fclose (fp);
  388.   return DV_SUCCESS;
  389. }
  390.  
  391.  
  392. /*---------------
  393.  *   LoadFilename  --  Load text file specified by user
  394.  *   for loading. Read the text information from the file.
  395.  */
  396. LOCAL int 
  397. LoadFilename ()
  398. {
  399.   int num_bytes_read, load_result;
  400.   FILE *fp;
  401.  
  402.   /*
  403.    * Check the filename to check that the filename is not null
  404.    * and the attempt to open the file for reading only.
  405.    */
  406.   if (strcmp (filename, "") == 0)
  407.     return DV_FAILURE;
  408.   fp = fopen (filename, "r");
  409.   if (fp == (FILE *) 0)
  410.     return DV_FAILURE;
  411.  
  412.   /*
  413.    * Read the file for the max number of bytes
  414.    * that can be stored in the buffer text_var. Then close
  415.    * the file.
  416.    */
  417.  
  418.   num_bytes_read = fread (text_var, 1, max_length, fp);
  419.  
  420.   if (num_bytes_read == max_length)
  421.     {
  422.       num_bytes_read -= 1;
  423.       load_result = TOO_LONG;
  424.     }
  425.   else
  426.     load_result = DV_SUCCESS;
  427.  
  428.   text_var[num_bytes_read] = '\0';
  429.   fclose (fp);
  430.  
  431.   return load_result;
  432. }
  433.